﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using School.Entities;
using School.Web.Models;
using School.Organization;

namespace School.Web.Controllers
{
    public class CertificateController : Controller
    {
        //
        // GET: /Certificate/

        public ActionResult Index()
        {
            var result = Structure.GetAllCertificates();
            List<CertificateViewModel> model = new List<CertificateViewModel>();
            foreach (ORG_Certificate c in result)
            {
                model.Add(new CertificateViewModel() { Id = c.Id, Name = c.Name, Description = c.Description, Campus_Id = c.Campus_Id });
            }
            return View(model);
        }

        //
        // GET: /Certificate/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Certificate/Create

        public ActionResult Create()
        {
            ViewBag.Campuses = Structure.GetAllCampuses();
            return View();
        } 

        //
        // POST: /Certificate/Create

        [HttpPost]
        public ActionResult Create(CertificateViewModel model)
        {
            try
            {
                Structure.AddCertificate(new ORG_Certificate() { Name = model.Name, Description = model.Description, Campus_Id = model.Campus_Id });

                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.Campuses = Structure.GetAllCampuses();
                return View(model);
            }
        }
        
        //
        // GET: /Certificate/Edit/5
 
        public ActionResult Edit(int id)
        {
            ORG_Certificate certificate = Structure.GetCertificate(id);
            CertificateViewModel model = new CertificateViewModel() { Id = certificate.Id, Campus_Id = certificate.Campus_Id, Name = certificate.Name, Description = certificate.Description };
            ViewBag.Campuses = Structure.GetAllCampuses();
            return View(model);
        }

        //
        // POST: /Certificate/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, CertificateViewModel model)
        {
            try
            {
                Structure.UpdateCertificate(new ORG_Certificate() { Id = id, Name = model.Name, Description = model.Description, Campus_Id = model.Campus_Id });                
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Certificate/Delete/5
 
        public ActionResult Delete(int id)
        {
            
            return View();
        }

        //
        // POST: /Certificate/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
